package com.pp.admin.common.server.entities;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.pp.admin.common.server.PersistenceManagerFactory;

/**
 * The persistent class for the PP_PHONES database table.
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Phone extends Base implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final byte TYPE_HOME = 1;

	public static final byte TYPE_OFFICE = 2;

	public static final byte TYPE_MOBILE = 3;

	public static final byte TYPE_OTHER = 4;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key id;

	@Persistent
	private String phoneNo;

	@Persistent
	private byte phoneType;

	@Persistent
	private boolean primary = false;

	@Persistent
	private Key createdBy;

	@Persistent
	private Date createdByTime;

	@Persistent
	private Key lastModefiedBy;

	@Persistent
	private Date lastModefiedByTime;

	// bi-directional many-to-one association to Contact
	@Persistent
	private Contact contact;

	public Phone() {

	}

	/**
	 * @return the id
	 */
	public Key getId() {

		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Key id) {

		this.id = id;
	}

	/**
	 * @return the phoneNo
	 */
	public String getPhoneNo() {

		return phoneNo;
	}

	/**
	 * @param phoneNo
	 *            the phoneNo to set
	 */
	public void setPhoneNo(String phoneNo) {

		this.phoneNo = phoneNo;
	}

	/**
	 * @return the phoneType
	 */
	public byte getPhoneType() {

		return phoneType;
	}

	/**
	 * @param phoneType
	 *            the phoneType to set
	 */
	public void setPhoneType(byte phoneType) {

		this.phoneType = phoneType;
	}

	/**
	 * @return the contact
	 */
	public Contact getContact() {

		return contact;
	}

	/**
	 * @param contact
	 *            the contact to set
	 */
	public void setContact(Contact contact) {

		this.contact = contact;
	}

	/**
	 * @return the primary
	 */
	public boolean isPrimary() {

		return primary;
	}

	/**
	 * @param primary
	 *            the primary to set
	 */
	public void setPrimary(boolean primary) {

		this.primary = primary;
	}

	/**
	 * @return the createdBy
	 */
	@Override
	public User getCreatedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory
				.getPersistenceManager();
		try {
			user = pm.getObjectById(User.class, this.createdBy);
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}

	/**
	 * @param createdBy
	 *            the createdBy to set
	 */
	@Override
	public void setCreatedBy(Key createdBy) {

		this.createdBy = createdBy;
	}

	/**
	 * @return the createdByTime
	 */
	@Override
	public Date getCreatedByTime() {

		return createdByTime;
	}

	/**
	 * @param createdByTime
	 *            the createdByTime to set
	 */
	@Override
	public void setCreatedByTime(Date createdByTime) {

		this.createdByTime = createdByTime;
	}

	/**
	 * @return the lastModefiedBy
	 */
	@Override
	public User getLastModefiedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory
				.getPersistenceManager();
		try {
			user = pm.getObjectById(User.class, this.lastModefiedBy);
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}

	/**
	 * @param lastModefiedBy
	 *            the lastModefiedBy to set
	 */
	@Override
	public void setLastModefiedBy(Key lastModefiedBy) {

		this.lastModefiedBy = lastModefiedBy;
	}

	/**
	 * @return the lastModefiedByTime
	 */
	@Override
	public Date getLastModefiedByTime() {

		return lastModefiedByTime;
	}

	/**
	 * @param lastModefiedByTime
	 *            the lastModefiedByTime to set
	 */
	@Override
	public void setLastModefiedByTime(Date lastModefiedByTime) {

		this.lastModefiedByTime = lastModefiedByTime;
	}

	public Phone importPhone(com.pp.admin.common.client.beans.Phone phone) {

		/**
		 * TODO: add server side validation for Phone type.
		 */
		this.phoneNo = phone.getPhoneNo();
		this.phoneType = phone.getPhoneType();
		this.primary = phone.isPrimary();
		return this;
	}

	public com.pp.admin.common.client.beans.Phone exportPhone() {
		com.pp.admin.common.client.beans.Phone phone = new com.pp.admin.common.client.beans.Phone();
		phone.setId(this.id.getId());
		phone.setPhoneNo(this.phoneNo);
		phone.setPhoneType(this.phoneType);
		phone.setPrimary(this.primary);
		return phone;
	}
}
